#!/usr/bin/env python2
# -*- coding: UTF-8 -*-

import os

import file_util as FileUtil

# flutter 的图片目录
imageFileDir = FileUtil.getLastDir(FileUtil.getCurDir())+"/assets/images"

# image_name_util.dart 文件输出目录
imageNameUtilDir = FileUtil.getCurDir()+"/util"

if(not FileUtil.isFileExit(imageNameUtilDir)):
    FileUtil.mkdirs(imageNameUtilDir)

# image_name_util.dart 文件
imageNameUtilFile = imageNameUtilDir+"/image_name_util.dart"

# 生成 image_name_util.dart 文件
# fieldArr : 属性名称
# fileNameArr : 图片名称
def genDartFile(fieldArr, fileNameArr):
    resultStr = "class ImageNameUtil {\n\n"

    imagePrefix = FileUtil.getFileName(FileUtil.getLastDir(imageFileDir))+"/"+FileUtil.getFileName(imageFileDir)

    for index, fileName in enumerate(fileNameArr):
        resultStr += 'static const String {} = "{}/{}";\n\n'.format(fieldArr[index], imagePrefix, fileName)

    resultStr += "}"

    FileUtil.saveFile(imageNameUtilFile, resultStr.decode("utf-8")) 

    print("gen file succes, out path : "+imageNameUtilFile)       


if __name__ == '__main__':
    fileArr = FileUtil.getFilePaths(imageFileDir)

    targetFileArr = []
    fileNameArr = []

    for filePath in fileArr:
        if ("/2.0x/" in filePath) or ("/3.0x/" in filePath):
            continue
        fileName = os.path.basename(filePath)
        fileNameArr.append(fileName)

        fieldNameArr = fileName.split(".")[0].split("-")

        finalFieldName = ""
        for index, filedName in enumerate(fieldNameArr):
            if index == 0:
                finalFieldName += filedName
            else:
                finalFieldName += filedName.title()
        targetFileArr.append(finalFieldName)

    genDartFile(targetFileArr, fileNameArr)